﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio;
using VSIXUtilities.DataObjects.Enums;
using VSIXUtilities.Helpers;

namespace VSIXUtilities.Loggers
{
	public static class StatusBarLogger
	{
		private static uint UserID = (uint)Constants.VSCOOKIE_NIL;
		private static IVsStatusbar IVsStatusbarInstance;
		public static IVsStatusbar StatusBar
		{
			get
			{
				if(IVsStatusbarInstance == null)
				{
					IVsStatusbarInstance = ServiceProvider.GlobalProvider.GetService(typeof(SVsStatusbar)) as IVsStatusbar;
				}
				return IVsStatusbarInstance;
			}
		}
		private static bool IsStatusBarFrozen
		{
			get
			{
				int frozen;
				StatusBar.IsFrozen(out frozen);
				return Convert.ToBoolean(frozen);
			}
		}

		public static int StartStatusBarAnimation(IntPtr IntPtrBitmapStrip)
		{
			if(!IsStatusBarFrozen)
			{
				object hdcObject = (object)IntPtrBitmapStrip;
				return StatusBar.Animation(1, ref hdcObject);
			}
			return VSConstants.S_FALSE;
		}
		public static int EndStatusBarAnimation(IntPtr IntPtrBitmapStrip)
		{
			if(!IsStatusBarFrozen)
			{
				object hdcObject = (object)IntPtrBitmapStrip;
				int Result = StatusBar.Animation(0, ref hdcObject);
				SafeNativeMethods.DeleteObject(IntPtrBitmapStrip);
				return Result;
			}
			return VSConstants.S_FALSE;
		}
		public static int LogStatusBarProgress(int fInProgress, string pwszLabel, uint nComplete, uint nTotal)
		{
			return StatusBar.Progress(ref UserID, 1, pwszLabel, nComplete, nTotal);
		}
		public static int Log(string Message)
		{
			if(!IsStatusBarFrozen)
			{
				return StatusBar.SetText(Message);
			}
			return VSConstants.S_FALSE;
		}
		public static int SetTextInsertionMode(UIE_TEXTINSMODE Mode)
		{
			object mode = Convert.ToInt32(Mode);
			return StatusBar.SetInsMode(ref mode);
		}
		public static int FreezeOutput(bool Freeze)
		{
			return StatusBar.FreezeOutput(Convert.ToInt32(Freeze));
		}
		public static int HideProgressBar()
		{
			return StatusBar.Progress(ref UserID, 0, string.Empty, 0, 0);
		}
	}
}
